<?php


namespace App\events;


use App\Conf;
use App\controller\choice\Manage;
use App\controller\RegisterController;
use App\event\TcpEventInterface;
use App\PackMsg;

class PrinterTcpServer implements TcpEventInterface
{
    public static function connect(\Swoole\Server $server, int $fd, int $reactorId)
    {
        printf("Connect");
        echo "Client: Connect.\n";
    }


    public static function receive(\Swoole\Server $server, int $fd, int $reactorId, string $data)
    {
        printf("%s:\t%s", date("Y-m-d H:i:s", time()), $data);
        $msg = (new Manage(PackMsg::unPack($data), $fd, $server))->swithPack();
        $server->send($fd, PackMsg::pack($msg->getType(), $msg->getData()));
        printf("发送数据ingg\n");
    }


    public static function close(\Swoole\Server $server, int $fd, int $reactorId)
    {
        if ($server->table->exist(strval($fd))) {
            $server->table->del(strval($fd));
        }
        printf("Client: Close");
    }


    public static function shutdown(\Swoole\Server $server)
    {
//        RegisterController::clearFd();
        printf("shutdown");
    }
}

